package main
import "fmt"

func main() {
    // 初始化切片
    var numbers = make([]int, 3, 5)

    printSlice(numbers)

    var numbers1 []int

    // 空切片
    printSlice(numbers1)

    if (numbers1 == nil) {
        fmt.Println("切片是空的")
    }

    numbers2 := []int {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    printSlice(numbers2)

    fmt.Println("numbers2 == ", numbers2)
    // 打印切片从索引1(包含)到索引4（不包含）
    fmt.Println("numbers2[1:4]", numbers2[1:4])
    // 打印切片下限为0
    fmt.Println("numbers2[:3]", numbers2[:3])
    // 打印切片上线为len(s)
    fmt.Println("numbers2[4:]", numbers2[4:])

    numbers3 := make([]int, 0, 5)
    printSlice(numbers3)

    numbers4 := numbers2[:2]
    printSlice(numbers4)

    numbers5 := numbers2[2:5]
    printSlice(numbers5)

    var numbers6 []int
    printSlice(numbers6)

    // 允许追加空切片
    numbers6 = append(numbers6, 0)
    printSlice(numbers)

    // 向切片添加一个元素
    numbers6 = append(numbers6, 1)
    printSlice(numbers6)

    // 同时添加多个元素
    numbers6 = append(numbers6, 2, 3, 4)
    printSlice(numbers6)

    // 创建切片 numbers7 是之前的切片的两倍
    numbers7 := make([]int, len(numbers6), cap(numbers6)*2)
    printSlice(numbers7)

    // 拷贝numbers7的内容到numbers8
    copy(numbers7, numbers6)
    printSlice(numbers7)
}

func printSlice(x []int) {
    fmt.Printf("len=%d, cap=%d slice=%v\n", len(x), cap(x), x)
}
